package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.Exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.ResultType;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private CityInfoService cityInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoService roomInfoService;
    @Override
    public void customSaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        //1.判断apartmentId是否为null
        boolean isNUll = apartmentSubmitVo.getId() != null;

        //设置省市区
        apartmentSubmitVo.setProvinceName(provinceInfoService.getById(apartmentSubmitVo.getProvinceId()).getName());
        apartmentSubmitVo.setCityName(cityInfoService.getById(apartmentSubmitVo.getCityId()).getName());
        apartmentSubmitVo.setDistrictName(districtInfoService.getById(apartmentSubmitVo.getDistrictId()).getName());

        //2.添加公寓的--saveOrUpdate()
        boolean b = saveOrUpdate(apartmentSubmitVo);
        //3.修改--删除四个关系表（图片、配套、标签、杂费）
        if (isNUll) {
            //1图片--条件 item_type item_id为出入进来的
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            lambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);
            //2.配套
            LambdaQueryWrapper<ApartmentFacility> wrapperAF = new LambdaQueryWrapper<>();
            wrapperAF.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(wrapperAF);
            //3.标签
            LambdaQueryWrapper<ApartmentLabel> wrapperAL = new LambdaQueryWrapper<>();
            wrapperAL.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(wrapperAL);
            //4.杂费
            LambdaQueryWrapper<ApartmentFeeValue> wrapperFee = new LambdaQueryWrapper<>();
            wrapperFee.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(wrapperFee);
        }
        //4.添加四个关系的--save()
        //图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().facilityId(facilityInfoId).
                        apartmentId(apartmentSubmitVo.getId()).build();
                apartmentFacilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

        //标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue build = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId).build();
                apartmentFeeValues.add(build);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
    }

    @Override
    public void customPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.customSelectPage(page, queryVo);
    }

    @Override
    public ApartmentDetailVo customSelect(Long id) {
        //查询公寓的信息
        ApartmentInfo apartmentInfo = getById(id);
        //图片信息
        List<GraphVo> graphVoList = graphInfoMapper.customGet(id, ItemType.APARTMENT);
        //标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.customGet(id, ItemType.APARTMENT);
        //配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.customGet(id);
        //杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.customGet(id);
        //生成VO
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //ApartmentInfo的VO整合
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        return apartmentDetailVo;
    }


    @Override
    public void customDeleted(Long id) {
        //判断当前公寓是否还有房间
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapperRI = new LambdaQueryWrapper<>();
        lambdaQueryWrapperRI.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(lambdaQueryWrapperRI);
        if (count>0){
            //当前公寓有房间
            //抛出异常
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR.getCode(),ResultCodeEnum.DELETE_ERROR.getMessage());
        }

        //删除公寓id
        removeById(id);

        //删除中间表
        //1图片--条件 item_type item_id为出入进来的
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        lambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(lambdaQueryWrapper);
        //2.配套
        LambdaQueryWrapper<ApartmentFacility> wrapperAF = new LambdaQueryWrapper<>();
        wrapperAF.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(wrapperAF);
        //3.标签
        LambdaQueryWrapper<ApartmentLabel> wrapperAL = new LambdaQueryWrapper<>();
        wrapperAL.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(wrapperAL);
        //4.杂费
        LambdaQueryWrapper<ApartmentFeeValue> wrapperFee = new LambdaQueryWrapper<>();
        wrapperFee.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(wrapperFee);
    }
}




